package net.lgy.controller;


import lombok.extern.slf4j.Slf4j;
import net.lgy.controller.request.AccountLoginRequest;
import net.lgy.dto.AccountDTO;
import net.lgy.enums.BizCodeEnum;
import net.lgy.model.AccountDO;
import net.lgy.service.AccountService;
import net.lgy.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@Slf4j
@RequestMapping("/api/account/v1")
public class AccountController {

    @Autowired
    private AccountService accountService;


//    文件上传接口

    @PostMapping("/upload_avatar")
    public JsonData uploadAvatar(@RequestParam("file")MultipartFile file) throws IOException {
        String url = accountService.uploadAvatar(file);


        return JsonData.buildSuccess(url);
    }

//    创建默认的存储空间（和注册联调），初始化根目录,可以用消息队列接受
    @GetMapping("/createDefaultBucket")
    public JsonData createDefaultBucket(@RequestBody AccountDO accountDO){
        accountService.createDefaultBucket(accountDO);

        return JsonData.buildSuccess();
    }

//    登陆模块
    @PostMapping("/login")
    public JsonData login(@RequestBody AccountLoginRequest request){

        AccountDTO accountDTO = accountService.login(request);
        if (accountDTO == null){
            return  JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
        }
        String token = accountDTO.getToken();
        return JsonData.buildSuccess(token);
    }


//    根据手机号查找信息
    @PostMapping("/find_by_phone")
    public JsonData findByPhone(@RequestParam("phone") String phone){
        AccountDTO accountDTO = accountService.findByPhone(phone);
        return JsonData.buildSuccess(accountDTO);
    }




}
